package org.jeecg.modules.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.business.entity.BusMarketAgreementCustomer;
import org.jeecg.modules.business.entity.BusMarketAgreementCustomerHousePrice;
import org.jeecg.modules.business.enums.CommissionUnitEnum;
import org.jeecg.modules.business.mapper.BusMarketAgreementCustomerHousePriceMapper;
import org.jeecg.modules.business.mapper.BusMarketAgreementCustomerMapper;
import org.jeecg.modules.business.service.IBusMarketAgreementCustomerService;
import org.jeecg.modules.rooms.entity.CesRoomLayout;
import org.jeecg.modules.rooms.mapper.CesRoomLayoutMapper;
import org.jeecg.modules.system.entity.*;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 协议单位-客户协议
 * @Author: jeecg-boot
 * @Date:   2023-04-11
 * @Version: V1.0
 */
@Service
public class BusMarketAgreementCustomerServiceImpl extends ServiceImpl<BusMarketAgreementCustomerMapper, BusMarketAgreementCustomer> implements IBusMarketAgreementCustomerService {
    @Resource
    private CesRoomLayoutMapper cesRoomLayoutMapper;

    @Resource
    private BusMarketAgreementCustomerHousePriceMapper agreementCustomerHousePriceMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCustomer(BusMarketAgreementCustomer agreementCustomer,String tenantId) {
        //step.1 保存客户协议
        this.save(agreementCustomer);
        //step.2 处理协议关联房价信息
        // 获取到所有房型
        List<CesRoomLayout> layoutList = cesRoomLayoutMapper.selectList(new QueryWrapper<CesRoomLayout>()
                .eq(CesRoomLayout.HOTEL_ID, agreementCustomer.getHotelId())
                .eq(CesRoomLayout.INVALID,false));
        List<BusMarketAgreementCustomerHousePrice> customerHousePriceList = new ArrayList<>();
        if (layoutList != null && layoutList.size() >0) {
            layoutList.forEach(item -> {
                BusMarketAgreementCustomerHousePrice customerHousePrice = new BusMarketAgreementCustomerHousePrice();
                customerHousePrice.setTenantId(tenantId);
                customerHousePrice.setHotelId(agreementCustomer.getHotelId());
                customerHousePrice.setCustomerId(agreementCustomer.getId());
                customerHousePrice.setLayoutId(item.getId());
                customerHousePrice.setAgreementPrice(item.getMarketPrice());
                customerHousePrice.setAgreementDiscount(BigDecimal.valueOf(100));
                customerHousePrice.setDiscount(BigDecimal.valueOf(100));
                customerHousePrice.setBreakfastNumber(0);
                customerHousePrice.setMonthPrice(BigDecimal.valueOf(0));
                customerHousePrice.setCommissionUnit(CommissionUnitEnum.yuan.getKey());
                customerHousePrice.setDailyCommission(BigDecimal.valueOf(0));
                customerHousePrice.setDelFlag(CommonConstant.DEL_FLAG_0);
                customerHousePriceList.add(customerHousePrice);
                agreementCustomerHousePriceMapper.insert(customerHousePrice);
            });
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editCustomer(BusMarketAgreementCustomer agreementCustomer,List<BusMarketAgreementCustomerHousePrice> customerHousePriceList) {
        //step.1 修改客户协议基础信息
        this.updateById(agreementCustomer);

        //step.2 修改关联协议房价信息
        if (customerHousePriceList != null && customerHousePriceList.size() > 0){
            customerHousePriceList.forEach(item->{
                agreementCustomerHousePriceMapper.updateById(item);
            });
        }
//        //查询已关联房价信息
//        List<BusMarketAgreementCustomerHousePrice> customerHousePriceList = agreementCustomerHousePriceMapper
//                .selectList(new QueryWrapper<BusMarketAgreementCustomerHousePrice>()
//                        .lambda().eq(BusMarketAgreementCustomerHousePrice::getCustomerId, agreementCustomer.getId()));
//        if(customerHousePriceList != null && customerHousePriceList.size()>0){
//            for(BusMarketAgreementCustomerHousePrice housePrice : customerHousePriceList ){
//            }
//        }
    }
}
